package com.example.myshortcutdemo.loading;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.TypedValue;
import android.view.animation.AccelerateDecelerateInterpolator;

import androidx.annotation.AttrRes;
import androidx.annotation.NonNull;

import java.util.ArrayList;

/**
 * @author tgw
 * @date 2022/5/9
 * @describe
 *局部加载的loading样式，三个小球往右循环移位同时大小透明度变化
 * 动画规则：1号球运动轨迹[r,w/2,w-r,w]
 * 2号球运动轨迹[w/2,w-r,w,3w/2-r]
 * 3号球运动轨迹[w-r.w,3w/2-r,2(w-r)]
 * 其中w代表getWidth ，r代表半径radius
 */
public class BallCircleIndicator extends Indicator {

    //位置偏移量
    private final float[] offsetValue = new float[3];
    private final int centerColor;
    private final int leftColor;

    public BallCircleIndicator() {
        centerColor = Color.parseColor("#00ba9e");
        leftColor =Color.parseColor("#a8e1d8");
    }

    @Override
    public void draw(Canvas canvas, Paint paint) {
        //小球的大小半径,设置为宽度的1/10
        float radius = (float) getWidth() / 10;
        //绘制第一个小球
        canvas.save();
        setColor(leftColor);
        paint.setAlpha(76);
        //计算第一个小球的位置和半径
        //第四个辅助小球的半径
        float assistRadius;
        if (offsetValue[0] * getWidth() > getWidth() - radius) {
            assistRadius = offsetValue[0] * getWidth() - getWidth() + radius;
            canvas.drawCircle(getWidth() - radius, getHeight() >> 1, getWidth() - offsetValue[0] * getWidth(), paint);
            //同时绘制辅助小球
            canvas.drawCircle(radius, getHeight() >> 1, assistRadius, paint);
        } else
            canvas.drawCircle(offsetValue[0] * getWidth(), getHeight() >> 1, radius, paint);
        canvas.restore();

        //绘制第二个小球
        canvas.save();
        setColor(centerColor);
        paint.setAlpha(255);
        if (offsetValue[1] * getWidth() <= getWidth() - radius)
            canvas.drawCircle(offsetValue[1] * getWidth(), getHeight() >> 1, radius, paint);
        else if (offsetValue[1] * getWidth() > getWidth() - radius && offsetValue[1] * getWidth() <= getWidth()) {
            assistRadius = offsetValue[1] * getWidth() - getWidth() + radius;
            canvas.drawCircle(getWidth() - radius, getHeight() >> 1, getWidth() - offsetValue[1] * getWidth(), paint);
            //同时绘制辅助小球
            canvas.drawCircle(radius, getHeight() >> 1, assistRadius, paint);
        } else if (offsetValue[1] * getWidth() > getWidth()) {
            canvas.drawCircle(offsetValue[1] * getWidth() + radius - getWidth(), getHeight() >> 1, radius, paint);
        }
        canvas.restore();

        //绘制第三个小球
        canvas.save();
        setColor(Color.parseColor("#60d1c0"));
        paint.setAlpha(153);
        if (offsetValue[2] * getWidth() <= getWidth()) {
            assistRadius = offsetValue[2] * getWidth() - getWidth() + radius;
            canvas.drawCircle(getWidth() - radius, getHeight() >> 1, getWidth() - offsetValue[2] * getWidth(), paint);
            //同时绘制辅助小球
            canvas.drawCircle(radius, getHeight() >> 1, assistRadius, paint);
        } else
            canvas.drawCircle(offsetValue[2] * getWidth() + radius - getWidth(), getHeight() >> 1, radius, paint);
        canvas.restore();
    }

    @Override
    public ArrayList<ValueAnimator> onCreateAnimators() {
        ArrayList<ValueAnimator> animators = new ArrayList<>();
        ValueAnimator translationAnimator;
        for (int i = 0; i < 3; i++) {
            final int index = i;
            if (i == 0)
                translationAnimator = ValueAnimator.ofFloat(0.1f, 0.5f, 0.9f, 1f);
            else if (i == 1)
                translationAnimator = ValueAnimator.ofFloat(0.5f, 0.9f, 1f, 1.4f);
            else
                translationAnimator = ValueAnimator.ofFloat(0.9f, 1f, 1.4f, 1.8f);
            translationAnimator.setDuration(2000);
            translationAnimator.setRepeatCount(-1);
            translationAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
            addUpdateListener(translationAnimator, new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    offsetValue[index] = (float) animation.getAnimatedValue();
                    postInvalidate();
                }
            });
            animators.add(translationAnimator);
        }
        return animators;
    }

    private int getColorValue(@NonNull Context context, @AttrRes int attrId) {
        TypedValue typedValue = new TypedValue();
        context.getTheme().resolveAttribute(attrId, typedValue, true);
        return typedValue.data;
    }
}
